package com.my9yu.common.socket;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.my9yu.common.socket.core.Message;
import com.my9yu.common.socket.util.JsonUtils;

public class ServerHander extends IoHandlerAdapter implements IoHandler {
	private static Logger Logger = LoggerFactory.getLogger(Server.class);

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		Logger.info("打开连接:" + session.getRemoteAddress());
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		if (!(message instanceof Message)) {
			Logger.info("请求格式不对,内容是：  " + JsonUtils.object2String(message));
			Logger.info("请求格式不对,对象类型是：  " + message.getClass().getSimpleName());
			Message result = new Message();
			result.setBody("FAIL".getBytes());
			session.write(result);
			return;

		}
		Logger.info("接受请求:" + JsonUtils.object2String(message));
		Message result = new Message();
		result.setBody("OK".getBytes());
		session.write(result);
	}

}
